package com.tamplus.cs.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class BusinessUtils {
	public static final String OPER_LESS_THAN			= "<";
	public static final String OPER_LESS_THAN_EQUALS	= "<=";
	public static final String OPER_MORE_THAN			= ">";
	public static final String OPER_MORE_THAN_EQUALS	= ">=";
	public static final String OPER_EQUALS				= "=";
	public static final String FLAG_Y 					= "Y";
	public static final String FLAG_N 					= "N";
	public static final String FLAG_NULL 				= null;
	
	public static String getFlag(String flag){
		if("true".equalsIgnoreCase(flag)) return FLAG_Y;
		else return FLAG_N;
	}	
	
	//calculate age
	public static String calculateAge(Date birthDate){
		System.out.println("Start calculateAge...");
//		Date date = DateUtils.createDate(dateStr, "dd/MM/yyyy");
		Calendar cNow 	= Calendar.getInstance(Locale.US);
		Calendar c 		= Calendar.getInstance(Locale.US);
		c.setTime(birthDate);
		
		int age = 0;
        if(c.after(cNow)) {
            throw new IllegalArgumentException("Can't be born in the future");
        }
        age = cNow.get(Calendar.YEAR) - c.get(Calendar.YEAR);             
        if(cNow.get(Calendar.DAY_OF_YEAR) < c.get(Calendar.DAY_OF_YEAR))  {
            age-=1;
        }
		
		System.out.println("age : "+age);		
		System.out.println("End calculateAge...");		
		return ""+age;
	}
	
	public static String checkAge(String ageStr, String ageConStr, String condition){
		System.out.println("Start checkAge...");		
		String flag = FLAG_N;
		System.out.println("condition : "+(ageStr + condition + ageConStr));
		if(ageStr == null || "".equals(ageStr)) return FLAG_N;
		
		int age 	= StringUtils.parseInt(ageStr);
		int ageCon 	= StringUtils.parseInt(ageConStr);
		if(OPER_LESS_THAN.equals(condition) && age < ageCon) flag = FLAG_Y;
		else if(OPER_LESS_THAN_EQUALS.equals(condition) && age <= ageCon) flag = FLAG_Y;
		else if(OPER_MORE_THAN.equals(condition) && age > ageCon) flag = FLAG_Y;
		else if(OPER_MORE_THAN_EQUALS.equals(condition) && age >= ageCon) flag = FLAG_Y;
		else if(OPER_EQUALS.equals(condition) && age == ageCon) flag = FLAG_Y;
		
		System.out.println("flag : "+flag);
		System.out.println("End checkAge...");		
		return flag;
	}
	
	public static String checkAgeScope(String ageStr, String ageFromStr, String ageToStr){
		System.out.println("Start checkAgeScope...");		
		String flag = FLAG_N;
		System.out.println("condition : "+(ageStr + " in (" + ageFromStr + "-" + ageToStr + ")"));
		if(ageStr == null || "".equals(ageStr)) return FLAG_N;
		
		int age 	= StringUtils.parseInt(ageStr);
		int ageFrom = StringUtils.parseInt(ageFromStr);
		int ageTo 	= StringUtils.parseInt(ageToStr);
		if(age >= ageFrom && age <= ageTo) flag = FLAG_Y;
		
		System.out.println("flag : "+flag);
		System.out.println("End checkAgeScope...");		
		return flag;
	}	
	
	public static void main(String[] args){
//		BusinessUtils.calculateAge("07/04/1981");
		
		BusinessUtils.checkAge("31", "33", OPER_LESS_THAN);
		BusinessUtils.checkAgeScope("36", "33", "45");
		
	}
	
}

